package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.goods.IGoodsService;
import com.iwhalecloud.bss.kite.cucc.client.dto.goods.GoodPriceAttrReqDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.goods.GoodPriceReqDTO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.StrTools;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.QueryPriceAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Classname QueryPriceAttrLinkHandler
 * @Description SDWAN产品变更-带宽变更，选择带宽后，查询价格
 * @Author zhang.song
 * @Date 2021-04-07 15:09
 */
@Service
@AttrHandler(QueryPriceAttrHandler.class)
public class QueryPriceAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String sceneInstId = data.getSceneInstId();
        // 变更业务的场景初始化过程中 跳过
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(data.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(sceneInstId, LocalKeyConsts.BUSI_CHANGE_SCENE))
            && !"workOrder".equals(querySceneInstService.getExtParameterValue(sceneInstId, "initSource"))) {
            return;
        }
        //重提初始化 跳过
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (Objects.nonNull(extParams) && extParams.containsKey("nodeCode")) {
            return;
        }

        if(ServiceOfferConsts.SERVICE_OFFER_MODIFY.equals(data.getServiceOfferId())) {
            if (KiteStringUtils.isNotEmpty(attr.getValue())) {
                List<KiteAttr> kiteAttr = inst.listAttrs();
                GoodPriceAttrReqDTO goodPriceAttrReq;
                List<GoodPriceAttrReqDTO> propertyValue = Lists.newArrayList();
                for (KiteAttr attrTemp : kiteAttr) {
                    if(KiteStringUtils.equals(attrTemp.getMakeField(), "T")) {
                        goodPriceAttrReq = new GoodPriceAttrReqDTO();
                        goodPriceAttrReq.setAttr(attrTemp.getFieldName());
                        goodPriceAttrReq.setAttrVal(attrTemp.getValue());
                        propertyValue.add(goodPriceAttrReq);
                    }
                }
                GoodPriceReqDTO goodPriceReq = new GoodPriceReqDTO();
                goodPriceReq.setSKU("10003406");
                goodPriceReq.setPropertyValue(propertyValue);
                // 查询价格
                DataResult<String> result = goodsService.queryGoodPrice(goodPriceReq);
                String price = result.getResultObject();
                if(StrTools.isFloat(price)) {
                    // 标准月租（元）
                    String fieldName = "200001354";
                    KiteAttr attr1 = inst.getAttr(fieldName);
                    if (Objects.nonNull(attr1)) {
                        if(KiteStringUtils.isEmpty(attr1.getValue())) {
                            attr1.setOriginValue(price);
                        }
                        operateInstAttrService.setAttrValue(inst, fieldName, price, data.getSceneInstId(), null);
                    }
                }
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
